package com.cicigodd.brick.plug.system.role.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cicigodd.brick.plug.common.exception.BusinessException;
import com.cicigodd.brick.plug.common.factory.PageFactory;
import com.cicigodd.brick.plug.common.factory.PageResultFactory;
import com.cicigodd.brick.plug.common.response.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cicigodd.brick.plug.system.role.entity.SysRoleMenu;
import com.cicigodd.brick.plug.system.role.response.SysRoleMenuDetailResponse;
import com.cicigodd.brick.plug.system.role.response.SysRoleMenuListResponse;
import com.cicigodd.brick.plug.system.role.request.SysRoleMenuAddRequest;
import com.cicigodd.brick.plug.system.role.request.SysRoleMenuDeleteRequest;
import com.cicigodd.brick.plug.system.role.request.SysRoleMenuDetailRequest;
import com.cicigodd.brick.plug.system.role.request.SysRoleMenuEditRequest;
import com.cicigodd.brick.plug.system.role.request.SysRoleMenuListRequest;
import com.cicigodd.brick.plug.system.role.mapper.SysRoleMenuMapper;
import com.cicigodd.brick.plug.system.role.service.SysRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

/**
 * 角色菜单关联
 *
 * @author cicigodd
 * @date 2022/12/12 15:52:23
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    /**
     * 添加
     *
     * @author cicigodd
     * @date 2022/12/12 15:52:23
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysRoleMenuAddRequest sysRoleMenuAddRequest) {
        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        BeanUtil.copyProperties(sysRoleMenuAddRequest, sysRoleMenu);
        this.save(sysRoleMenu);
    }

    /**
     * 删除
     *
     * @author cicigodd
     * @date 2022/12/12 15:52:23
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysRoleMenuDeleteRequest sysRoleMenuDeleteRequest) {
        this.removeByIds(sysRoleMenuDeleteRequest.getIds());
    }


    /**
     * 编辑
     *
     * @author cicigodd
     * @date 2022/12/12 15:52:23
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(SysRoleMenuEditRequest sysRoleMenuEditRequest) {
        SysRoleMenu sysRoleMenu = this.getById(sysRoleMenuEditRequest.getId());
        if (ObjectUtil.isEmpty(sysRoleMenu)) {
            throw new BusinessException("查询结果不存在");
        }
        BeanUtil.copyProperties(sysRoleMenuEditRequest, sysRoleMenu);
        this.updateById(sysRoleMenu);
    }

    /**
     * 详情
     *
     * @author cicigodd
     * @date 2022/12/12 15:52:23
     */
    @Override
    public SysRoleMenuDetailResponse detail(SysRoleMenuDetailRequest sysRoleMenuDetailRequest) {
        SysRoleMenu sysRoleMenu = this.getById(sysRoleMenuDetailRequest.getId());
        if (ObjectUtil.isEmpty(sysRoleMenu)) {
            throw new BusinessException("查询结果不存在");
        }
        SysRoleMenuDetailResponse sysRoleMenuDetailResponse = new SysRoleMenuDetailResponse();
        BeanUtil.copyProperties(sysRoleMenu, sysRoleMenuDetailResponse);
        return sysRoleMenuDetailResponse;
    }

    /**
     * 列表
     *
     * @author cicigodd
     * @date 2022/12/12 15:52:23
     */
    @Override
    public PageResult<SysRoleMenuListResponse> list(SysRoleMenuListRequest sysRoleMenuListRequest) {
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        Integer roleId = sysRoleMenuListRequest.getRoleId();
        Integer menuId = sysRoleMenuListRequest.getMenuId();

        wrapper.eq(ObjectUtil.isNotNull(roleId), SysRoleMenu::getRoleId, roleId);
        wrapper.eq(ObjectUtil.isNotNull(menuId), SysRoleMenu::getMenuId, menuId);
        IPage<SysRoleMenuListResponse> page = this.page(PageFactory.defaultPage(sysRoleMenuListRequest.getPageNum(),sysRoleMenuListRequest.getPageSize()), wrapper).convert(SysRoleMenu -> BeanUtil.copyProperties(SysRoleMenu, SysRoleMenuListResponse.class));
        return PageResultFactory.createPageResult(page);
    }

    /**
     * 根据角色id删除菜单权限
     * @param roleId
     */
    @Override
    public void deleteByRoleId(Integer roleId) {
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId,roleId);
        this.remove(wrapper);
    }

    /**
     * 根据角色id获取菜单id
     * @param roleId
     * @return
     */
    @Override
    public List<Integer> getMenuIdsByRoleId(Integer roleId) {
        return this.getBaseMapper().getMenuIdsByRoleId(roleId);
    }

    /**
     * 根据角色id获取权限标识
     * @param roleIds
     * @return
     */
    @Override
    public List<String> getPermsIdsByRoleIds(List<Integer> roleIds) {
        return this.getBaseMapper().getPermsIdsByRoleIds(roleIds);
    }
}
